package com.qiusuo0226.platform.controller;

import com.qiusuo0226.file.util.FileResource;
import com.qiusuo0226.mvc.util.JsonResultModel;
import com.qiusuo0226.platform.annotation.RequestRecord;
import com.qiusuo0226.platform.service.FileStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件仓库前端控制层
 * @author qiusuo
 * @date 2023/7/7 22:07
 */
@RestController
@RequestMapping("/interface/fileStore")
@Slf4j
public class FileStoreController {

    @Autowired
    private FileStoreService fileStoreService;

    /**
     * todo 上传单文件
     * @param file
     * @return
     */
    @PostMapping("/add")
    @RequestRecord
    public JsonResultModel add(MultipartFile file){
        if (null == file) {
            return JsonResultModel.paramsError("参数file缺失");
        }
        Long add = fileStoreService.add(file);
        return null != add ? JsonResultModel.success("上传成功", add) :
                JsonResultModel.failure("上传失败");
    }

    @GetMapping("/export/{iid}")
    @RequestRecord
    public FileResource export(@PathVariable("iid") long iid) {
        return fileStoreService.export(iid);
    }
}
